package com.example.workrecord.util.dingtalkopenapi.utils.aes;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DingWorkFlowServiceImpl {

    public String receiveCallback(String corpId, String signature, String timestamp, String nonce, String encrypt) throws DingTalkEncryptException {

        DingTalkEncryptor dingTalkEncryptor = null;
//        String plainText = null;
//
//        JSONObject gsonMap = JSONObject.parseObject(encrypt);
//        encrypt = (String) gsonMap.get("encrypt");
//
        dingTalkEncryptor = new DingTalkEncryptor("qwer123qweasd", "1234567890123456789012345678901234567890aaa",corpId);
//        plainText = dingTalkEncryptor.getDecryptMsg(signature, timestamp, nonce, encrypt);
//
//
//
//        JSONObject plainTextjson = JSONObject.parseObject(plainText);
//
//        System.out.println(plainTextjson.toJSONString());
//
//        String eventType = plainTextjson.getString("EventType");
//        String type = plainTextjson.getString("type");
//        String _corpId=plainTextjson.getString("CorpId");
//        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+plainTextjson);
//        if (eventType.equals("check_url")) {
//
//            System.out.println("check_url~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//
//        }

        long timeStampLong = Long.parseLong(timestamp);
        Map<String, String> jsonMap = null;
        try {
            jsonMap = dingTalkEncryptor.getEncryptedMap("success", timeStampLong, nonce);
        } catch (DingTalkEncryptException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        JSONObject json = new JSONObject();
        json.putAll(jsonMap);
        return json.toString();
    }
        /**
         *
         * @param signature
         * @param timestamp
         * @param nonce
         * @param encrypt
         * @return
         */
    public String receiveCallback(String signature, String timestamp, String nonce, String encrypt) throws DingTalkEncryptException {

       return this.receiveCallback("ding1a4b105d2211e79f35c2f4657eb6378f",signature,timestamp,nonce,encrypt);
    }

}
